package quartz_distributed;

import org.quartz.JobExecutionContext;
import org.quartz.SchedulerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * 服务定时轮询任务,负责轮询获取服务列表,创建线程执行健康检查
 *
 * @author yunlongpang 2017/11/21
 */
@Component
@EnableScheduling
public class HealthCheckTask extends QuartzJobBean {

    private final Logger logger = LoggerFactory.getLogger(HealthCheckTask.class);

    /**
     * 轮询时间间隔
     */
    public static final int POLL_FIXED_TIME = 60;

    /**
     * 轮询时间间隔单位
     */
    public static final int POLL_FIXED_TIME_UNIT = 1000;


    @Override
    protected void executeInternal(JobExecutionContext context) {
        try {
            SchedulerContext schedulerContext = context.getScheduler().getContext();

            logger.info("health check task start.");
            StopWatch watch = new StopWatch();
            watch.start("health diagnose");

            watch.stop();
            logger.info(watch.prettyPrint());
        } catch (Exception e) {
            logger.error("quartz scheduler exception.", e);
        }
    }

}
